package com.adopapa.nezhacloud.authc.service;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.adopapa.nezhacloud.authc.domain.DepartmentInfo;
import com.adopapa.nezhacloud.authc.domain.DepartmentInfoRepository;
import com.adopapa.nezhacloud.common.Tools;
import com.adopapa.nezhacloud.ddd.service.BaseDomainService;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;


@Service
public class DepartmentInfoService extends BaseDomainService<DepartmentInfo> {

	@Autowired
	private DepartmentInfoRepository departmentInfoRepository;

	public DepartmentInfo getDepartmentInfoById(String id) {
		Optional<DepartmentInfo> optional = departmentInfoRepository.findById(id);
		return optional.isPresent() ? optional.get() : null;
	}
	

	public Page<DepartmentInfo> getDepartmentInfoPage(int pageNo, int pageSize, DepartmentInfo departmentInfo) {
		PageRequest pageRequest = buildPageRequest(pageNo, pageSize, Order.desc("createTime"));
		Specification<DepartmentInfo> specification = buildSpecification(departmentInfo);
		Page<DepartmentInfo> page = departmentInfoRepository.findAll(specification, pageRequest);
		return page;
	}

	public DepartmentInfo saveDepartmentInfo(DepartmentInfo departmentInfo) {
		return departmentInfoRepository.saveAndFlush(departmentInfo);
	}

	public List<DepartmentInfo> saveDepartmentInfos(List<DepartmentInfo> departmentInfos) {
		return departmentInfoRepository.saveAllAndFlush(departmentInfos);
	}

	public void deleteDepartmentInfo(String id) {
		departmentInfoRepository.deleteById(id);
	}

	public void deleteDepartmentInfo(DepartmentInfo departmentInfo) {
		departmentInfoRepository.delete(departmentInfo);
	}

	public void deleteDepartmentInfos(String... ids) {
		departmentInfoRepository.deleteAllByIdInBatch(Arrays.asList(ids));
	}

	public void deleteDepartmentInfos(List<DepartmentInfo> departmentInfos) {
		departmentInfoRepository.deleteAllInBatch(departmentInfos);
	}

	@SuppressWarnings("serial")
	@Override
	public Specification<DepartmentInfo> buildSpecification(DepartmentInfo departmentInfo) {
		return new Specification<DepartmentInfo>() {
			@Override
			public Predicate toPredicate(Root<DepartmentInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicates = new LinkedList<>();
				if (!Tools.empty(departmentInfo.getDeptName())) {
					predicates.add(cb.like(root.get("deptName"), //
						"%".concat(departmentInfo.getDeptName()).concat("%")));
				}
				return cb.and(predicates.toArray(new Predicate[0]));
			}

		};
	}
	
	// predicates.add(cb.equal(cb.substring(root.get("datagram"), 1, 23), dataPrefix));

}
